#!/bin/bash

# ====================================================================
# Hero相关代码批量清理脚本
# 用途：快速修复pokermind-operator模块中的Hero方法调用编译错误
# ====================================================================

echo "🚀 开始批量清理Hero相关代码..."

# 设置目标目录
OPERATOR_DIR="pokermind-operator/src/main/java/com/pokermind/operator"

# 检查目录是否存在
if [ ! -d "$OPERATOR_DIR" ]; then
    echo "❌ 错误：找不到目录 $OPERATOR_DIR"
    exit 1
fi

echo "📂 目标目录: $OPERATOR_DIR"

# 批量替换Hero方法调用
echo "🔧 正在替换Hero方法调用..."

# getHero* 方法替换
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroPosition()/\"UNKNOWN\"/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroNetResult()/BigDecimal.ZERO/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroSeatNumber()/1/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroStackStart()/BigDecimal.valueOf(20000)/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroHoleCards()/null/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.getHeroWentToShowdown()/false/g' {} \;

# setHero* 方法注释
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.setHeroPosition([^)]*);/\/\* Hero method removed \*\/;/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/\.setHeroNetResult([^)]*);/\/\* Hero method removed \*\/;/g' {} \;

# 修复方法引用错误（Java 8 Stream API）
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/PokerHand::getHeroNetResult/h -> BigDecimal.ZERO/g' {} \;
find $OPERATOR_DIR -name "*.java" -exec sed -i.bak 's/PokerHand::getHeroPosition/h -> \"UNKNOWN\"/g' {} \;

echo "✅ Hero方法调用替换完成"

# 统计处理的文件
PROCESSED_FILES=$(find $OPERATOR_DIR -name "*.java.bak" | wc -l)
echo "📊 处理了 $PROCESSED_FILES 个Java文件"

# 清理备份文件
echo "🧹 清理备份文件..."
find $OPERATOR_DIR -name "*.java.bak" -delete

# 验证编译
echo "🔍 验证编译..."
mvn compile -pl pokermind-operator -q

COMPILE_RESULT=$?

if [ $COMPILE_RESULT -eq 0 ]; then
    echo "🎉 编译成功！Hero相关错误已修复"
    
    # 显示总结
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "  ✅ Hero清理完成总结"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "📁 处理目录: $OPERATOR_DIR"
    echo "📄 处理文件: $PROCESSED_FILES 个Java文件"
    echo "🔧 修复内容: Hero方法调用替换为默认值"
    echo "✅ 编译状态: 通过"
    echo ""
    
    # 下一步建议
    echo "🚀 下一步建议:"
    echo "1. 运行完整编译: mvn clean compile"
    echo "2. 执行数据库迁移: mysql < docs/ADD_MULTI_GAME_TYPE_SUPPORT.sql"
    echo "3. 测试多游戏类型: cd pokermind-serv-dse-tcp && ./test_multi_game_types.sh"
    echo ""
    
else
    echo "❌ 编译失败，仍有错误需要手动修复"
    echo ""
    echo "💡 建议:"
    echo "1. 查看编译错误: mvn compile -pl pokermind-operator"
    echo "2. 参考修复指南: docs/HERO_CLEANUP_BATCH_SCRIPT.md"
    echo "3. 手动修复剩余错误"
    echo ""
fi

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
